package com.erp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.comm.DataAndView;
import com.erp.comm.ResultCode;
import com.erp.pojo.Goods;
import com.erp.pojo.Provider;
import com.erp.service.GoodsService;
import com.erp.service.ProviderService;
import com.erp.vo.GoodsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;
    @Autowired
    private ProviderService providerService;

    @RequestMapping("loadAllGoods")
    public DataAndView loadAllGoods(GoodsVo goodsVo) {
        IPage<Goods>page = new Page<>(goodsVo.getPage(), goodsVo.getLimit());

        goodsService.page(page);
        //条件查询
        QueryWrapper<Goods> qw = new QueryWrapper<>();
        //判断条件是否为空
        if(goodsVo.getProviderid()!= null && goodsVo.getProviderid()!=0){
            qw.like("providerid",goodsVo.getProviderid());
        }
        if(goodsVo.getGoodsname()!= null){
            qw.like("goodsname",goodsVo.getGoodsname());
        }
        if(goodsVo.getProductcode()!= null){
            qw.like("productcode",goodsVo.getProductcode());
        }
        if(goodsVo.getPromitcode()!= null){
            qw.like("promitcode",goodsVo.getPromitcode());
        }
        if(goodsVo.getDescription()!= null){
            qw.like("description",goodsVo.getDescription());
        }
        if(goodsVo.getSize()!= null){
            qw.like("size",goodsVo.getSize());
        }

        goodsService.page(page,qw);

        //查询结果
        List<Goods> list = page.getRecords();
        List<Goods> list2 = new ArrayList<>();

        System.out.println(list);
        for (Goods good : list) {

            Integer pid = good.getProviderid();
            Provider p = providerService.getById(pid);
            good.setProvidername(p.getProvidername());
            list2.add(good);
        }

        return new DataAndView(list2);


    }
    // 删除http://localhost:8080/goods/deleteGoods
    @RequestMapping("deleteGoods")
    public ResultCode deleteGoods(Goods goods) {

        // 获取用户填写的数据
        try {
            boolean bool = goodsService.removeById(goods);

            System.out.println(bool);
            if (bool) {
                return ResultCode.SUCCESS_DELETE;
            } else {
                return ResultCode.FAIL_DELETE;
            }
        } catch (Exception e) {
            return ResultCode.FAIL_DELETE;
        }
    }
    //添加http://localhost:8080/goods/addGoods
    @RequestMapping("addGoods")
    public ResultCode addGoods(Goods goods) {
        //防止更新后 添加 id有值
        goods.setId(null);

        try {

            boolean bool = goodsService.save(goods);
            if (bool) {
                return ResultCode.SUCCESS_ADD;
            } else {
                return ResultCode.FAIL_ADD;
            }
        } catch (Exception e) {
            return ResultCode.FAIL_ADD;
        }
    }
    //更新 http://1ocalhost:8080/goods/updateGoods
    @RequestMapping("updateGoods")
    public ResultCode updateGoods(Goods goods) {
        //防止更新后 添加id有值
        try {
            boolean bool = goodsService.updateById(goods);
            if (bool) {
                return ResultCode.SUCCESS_UPDATE;
            } else {
                return ResultCode.FAIL_UPDATE;
            }
        } catch (Exception e) {
            return ResultCode.FAIL_ADD;
        }
    }

    // 查询所有商品
    //http://1ocalhost:8080/goods/loadA1lGoodsForSelect
    @RequestMapping("loadAllGoodsForSelect")
    public DataAndView loadAllGoodsForSelect() {

        //查询你所有商品   privoderName nul1
        List<Goods> list = goodsService.list();

        List<Goods> list2 = new ArrayList<>();
        for (Goods good : list) {
            //获取当前商品的供货商id
            Integer provideId = good.getProviderid();
            //通过供货商id 查对应的供货商
            Provider provider = providerService.getById(provideId);
            //设置当前的商品的供货商名称
            good.setProvidername(provider.getProvidername());
            list2.add(good);
        }
        return new DataAndView(list2);
    }

    @RequestMapping("loadGoodsByProviderId")
    public DataAndView loadGoodsByProviderId(Integer providerid){
    //通过previderid 查所有商品
    QueryWrapper<Goods> qw =new QueryWrapper<>();
    if(providerid !=0){
        qw.eq("providerid", providerid);
    }

    List<Goods> lists =goodsService.list(qw);
    List<Goods> list2 = new ArrayList<>();
    for(Goods good:lists) {
        Provider provider = providerService.getById(providerid);
        //设置当前的商品的供货商名称
        good.setProvidername(provider.getProvidername());
        list2.add(good);
    }

    return new DataAndView(lists);
    }



}
